package controller; 

import java.text.SimpleDateFormat;
import java.util.*;

import javax.annotation.Resource;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.TextMessage;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import service.ConsumerService;
import service.ProducerService;


/** 
* @author 朱宏亮
* @version 创建时间：2017年12月4日 下午5:04:40 
* 类说明 
*/
@Controller
public class AMQController {
    
    //队列名:朱宏亮队列
    @Resource(name="QueueDestination")
    private Destination QueueDestination;
    
    //订阅名:朱宏亮订阅
    @Resource(name="TopicDestination")
    private Destination TopicDestination;
    

    //队列消息生产者
    @Resource(name="producerService")
    private ProducerService producer;
    
    //队列消息消费者
    @Resource(name="consumerService")
    private ConsumerService consumer;
    
    @RequestMapping({"","index"})
    public String index(HttpServletRequest request){
        return "index"; 
    }
    
    @RequestMapping(value="/producer",method=RequestMethod.GET)
    public String producer(Model model){
        System.out.println("------------打开Queue发送页面");
        
        Date now = new Date(); 
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = dateFormat.format( now );    
        
        model.addAttribute("time", time);
        
        return "producer";
    }
    
    @RequestMapping(value="/topicProducer",method=RequestMethod.GET)
    public String producer2(Model model){
        System.out.println("------------打开Topic发送页面");
        
        Date now = new Date(); 
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = dateFormat.format( now ); 
        
        model.addAttribute("time", time);
        
        return "topicProducer";
    }
    
    @RequestMapping(value="/onsend",method=RequestMethod.POST)
    public String producer(@RequestParam("message") String message, Model model) {
        
        System.out.println("------------正在发送 Queue");
        
        producer.sendMessage(QueueDestination, message);
        
        return "index";
    }
    
    @RequestMapping(value="/onTopicSend",method=RequestMethod.POST)
    public String producer2(@RequestParam("message") String message, Model model) {
        
        System.out.println("------------正在发送 Topic");
        
        producer.sendTopicMessage(TopicDestination, message);
        
        return "index";
    }
    
    @RequestMapping(value="/receive",method=RequestMethod.GET)
    public String queue_receive(Model model) throws JMSException {
        System.out.println("------------接收消息");
        
        TextMessage tm = consumer.receive(QueueDestination);
        model.addAttribute("textMessage", tm.getText());
 
        return "receive";
    }
    
 
}
 